<?php
/**
 * 分类管理控制器
 *
 * @Author: yuanji <fenglangyj@139.com>
 * @DateTime: 12-11-3 下午1:44
 * @Copyright (c) 2012 http://www.magentoui.com All rights reserved.
 * @Version  $Id: sss.php,v 1.0 uw Exp $
 * @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 */
class CategoryAction extends CommonAction {
	public $type = 1;//分类类型默认为图片分类
	public function _initialize() {//更改数据读取时的表前缀！
		parent::_initialize();//调用父类初始化方法(权限检查);
		C('DB_PREFIX','bizi_');//设置前缀
		if(is_numeric($_REQUEST['type'])){
			$this->type = $_REQUEST['type'];//设置分类类型
		}
		$this->assign('type',$this->type);
	}

    /**
     * 添加分类
     */
    public function add(){
		$Category = M('Category','CategoryModel');
		$tree = $Category->cat_list(0,0,true,0,$this->type);
		$this->assign("tree",$tree);
		$Category->clear_cache($this->type);//清楚分类缓存
		parent::add('Category:add');
	}
	/**
	*	插入数据
	*/
	/*public function insert(){
		//$_REQUEST['cat_ids'] = implode(',',$_REQUEST['cat_ids']);
	}*/
	/**
	*	编辑数据
	*/
	public function edit(){
		$cat_id = trim($_REQUEST['cat_id']);
		$Category = M('Category','CategoryModel');
		$vo = $Category->getByCatId($cat_id);
		$tree = $Category->cat_list(0,$vo['parent_id'],true,0,$this->type);
		$this->assign("tree",$tree);
		$this->assign ( 'vo', $vo );
		$this->display('Category:edit');
	}
	/*
	*	更新数据
	*/
	public function update(){
		/* 判断上级目录是否合法 */
		$cat_id = trim($_REQUEST['cat_id']);
		$Category = M('Category','CategoryModel');
		$children = array_keys($Category->cat_list($cat_id, 0, false,0,$this->type));     // 获得当前分类的所有下级分类
		if (in_array($_REQUEST['parent_id'], $children)){
			/* 选定的父类是当前分类或当前分类的下级分类 */
			$this->error('选定的父类是当前分类或当前分类的下级分类');
		}
		$Category->clear_cache($this->type);//清楚分类缓存
		parent::update();
	}
	public function index(){
		$Category = M('Category','CategoryModel');
		$cat_info = $Category->cat_list(0,0,false,0,$this->type);//获取主题分类列表
		$this->assign("cat_info",$cat_info);
		$this->display('Category:index');
	}
	/**
	*	ajax修改排序
	*/
	public function editSortOrder(){
		$cat_id = trim($_REQUEST['id']);
		$sort_order = trim($_REQUEST['val']);
		if(!is_numeric($cat_id) or !is_numeric($sort_order)){
			$this->ajaxReturn($sort_order,'必须输入一个数字！',0);		
		}
		
		$Category = M('Category','CategoryModel');
		$Category->cat_id = $cat_id;
		$Category->sort_order = $sort_order;
		if($Category->save()){
			$Category->clear_cache($this->type);//清楚分类缓存
			$this->ajaxReturn($sort_order,'修改成功!',1);
		}else{
			$this->ajaxReturn($sort_order,'修改失败!',0);
		}
	}
	/**
	*	修改"是否显示"状态
	*/
	public function toggleIsShow(){
		$id = trim($_REQUEST['id']);
		$val = trim($_REQUEST['val']);
		if(!is_numeric($id) or !is_numeric($val)){
			$this->ajaxReturn($val,'必须输入一个数字！',0);		
		}
		$Category = M('Category','CategoryModel');
		$Category->cat_id = $id;
		$Category->is_show = $val;
		if($Category->save()){
			$Category->clear_cache($this->type);//清楚分类缓存
			$this->ajaxReturn($val,'修改成功!',1);
		}else{
			$this->ajaxReturn($val,'修改失败!',0);
		}
	}
	public function setField(){
		$Category = M('Category','CategoryModel');
		$Category->clear_cache($this->type);//清除分类缓存
		parent::setField();
	}
	/**
	*	修改“是否显示到导航栏”状态
	*/
	public function toggleShowInNav(){
		$id = trim($_REQUEST['id']);
		$val = trim($_REQUEST['val']);
		if(!is_numeric($id) or !is_numeric($val)){
			$this->ajaxReturn($val,'必须输入一个数字！',0);		
		}
		$Category = M('Category','CategoryModel');
		$Category->cat_id = $id;
		$Category->show_in_nav = $val;
		if($Category->save()){
			$Category->clear_cache($this->type);//清楚分类缓存
			$this->ajaxReturn($val,'修改成功!',1);
		}else{
			//echo $Category->getLastSql();
			$this->ajaxReturn($val,'修改失败!',0);
		}
	}
	/**
	*	删除分类
	*/
	public function foreverdelete(){
		$id = trim($_REQUEST['id']);
		$Category = M('Category','CategoryModel');
		$Cat = $Category->where('cat_id = '.$id)->find();
		if(empty($Cat['cat_name'])){
			$this->error('你选择的分类不存在！');return false;
		}
		//检测分类下是否还有数据
		if($Cat['type'] == 1 ){//如果是图片分类，查询图片分类下面是否有图片
			$M = M('bizi_node');
			$list = $M->where("FIND_IN_SET($id,cat_ids)")->find();
			//$this->error($M->getLastSql());
		}elseif($Cat['type'] == 2 ){//如果是主题
			$M = M('bizi_theme');
			$list = $M ->where("FIND_IN_SET($id,cat_ids)")->find();
		}
		if($list){
			$this->error('你删除的分类下还有数据,不能删除!');return false;
		}
		
		//查询是否有子分类
		$list = $Category->where('parent_id = '.$id)->limit(1)->select();
		if($list){
			$this->error('你删除的分类不是末级分类,不能删除!');return false;
		}
		
		$res = $Category->where('cat_id = '.$id)->delete();
		if($res){
			$Category->clear_cache($this->type);//清楚分类缓存
			$this->success('删除成功！');
		}
	}
	/**
	*	复写获取模型名称
	*/
	public function getActionName(){
		return 'Category';
	}
}